<?php
/**
 * Created by PhpStorm.
 * User: qv
 * Date: 2018/7/27 0027
 * Time: 10:36
 */

namespace Database\base;

abstract class DbBase
{

    use DbEvent;

    /**
     * DbBase constructor.
     * @param array $config
     */
    public function __construct(array $config = [])
    {
        foreach ($config as $key => $val) {
            $this->$key = $val;
        }
    }


    /**
     * @param $name
     * @param $value
     * @return mixed
     * @throws DbException
     */
    public function __set($name, $value)
    {
        $method = 'set' . ucfirst($name);
        if (method_exists($this, $method)) {
            return $this->{$method}($value);
        } else if (property_exists($this, $name)) {
            return $this->$name = $value;
        } else if (method_exists($this, 'get' . ucfirst($name))) {
            throw new DbException('Set only-read property: ' . $name);
        } else {
            throw new DbException('Set unknown property: ' . $name);
        }
    }
}